package pacoteDao;

import java.sql.*;
import java.util.*;
import projetofinal.ConnectionFactory;
import projetofinal.Professor;

public class ProfessorDAO {

    private ConnectionFactory conexao = new ConnectionFactory();
    private Connection connection;
    String query;

    public ProfessorDAO() throws SQLException {
        if (connection == null || connection.isClosed()) {
            this.connection = conexao.getConnection();
        }
    }

    public boolean insert(Professor professor) throws SQLException {

        query = "insert into Professor(id,nome,cpf,rg,matricula,salario,ddd,telefone) values (" + professor.getId() + ",'" + professor.getNome() + "','" + professor.getCpf() + "','" + professor.getRg() + "'," + professor.getMatricula() + "'," + professor.getSalario() + ",'" + professor.getDdd() + "','" + professor.getTelefone() + "')";
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean update(Professor professor) throws SQLException {

        query = "update Professor set nome = '" + professor.getNome() + "', cpf = '" + professor.getCpf() + "', rg = '" + professor.getRg() + "', matricula = '" + professor.getMatricula() + "', salario = " + professor.getSalario() + ", ddd = '" + professor.getDdd() + "', telefone = '" + professor.getTelefone() + "' where id = " + professor.getId();
        Integer linha = conexao.executarNoQuery(query);

        conexao.close();
        return linha > 0;

    }

    public boolean delete(Professor professor) throws SQLException {

        query = "delete from Professor where id = " + professor.getId();
        int linhas = conexao.executarNoQuery(query);

        conexao.close();
        return linhas > 0;

    }

    public List<Professor> consultar() throws SQLException {

        query = "select * from Professor ";
        ResultSet rset = conexao.executar(query);

        List<Professor> professors = null;
        while (rset.next()) {
            professors = new ArrayList<Professor>();
            Professor professor = new Professor();
            professor.setId(rset.getInt(1));
            professor.setNome(rset.getString(2));
            professor.setCpf(rset.getString(3));
            professor.setRg(rset.getString(4));
            professor.setMatricula(rset.getString(5));
            professor.setSalario(rset.getInt(6));
            professor.setDdd(rset.getString(7));
            professor.setTelefone(rset.getString(8));
            professors.add(professor);
        }

        conexao.close();
        return professors;

    }

    public Professor getById(Integer id) throws SQLException {

        query = "select * from Professor where id = " + id;
        ResultSet rs = conexao.executar(query);

        Professor professor = null;
        if (rs.next()) {
            professor = new Professor();
            professor.setId(rs.getInt(1));
            professor.setNome(rs.getString(2));
            professor.setCpf(rs.getString(3));
            professor.setRg(rs.getString(4));
            professor.setMatricula(rs.getString(5));
            professor.setSalario(rs.getInt(6));
            professor.setDdd(rs.getString(7));
            professor.setTelefone(rs.getString(8));
        }

        conexao.close();
        return professor;

    }

    public Professor getByNome(String nome) throws SQLException {

        query = "select * from Professor where nome = '" + nome + "'";
        ResultSet rs = conexao.executar(query);

        Professor professor = null;
        if (rs.next()) {
            professor = new Professor();
            professor.setId(rs.getInt(1));
            professor.setNome(rs.getString(2));
            professor.setCpf(rs.getString(3));
            professor.setRg(rs.getString(4));
            professor.setMatricula(rs.getString(5));
            professor.setSalario(rs.getInt(6));
            professor.setDdd(rs.getString(7));
            professor.setTelefone(rs.getString(8));
        }

        conexao.close();
        return professor;

    }
}
